package org.rhok.pdx.ncn.background;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.wifi.WifiManager;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

public class TrackingService extends Service {

    private LocationListener locListener;
    private PhoneStateListener psListener;
    
    private ServerSyncerThread serverSyncer;
    private WifiSignalTrackerTask wifiTracker;
    
    private LocationManager locManager;
    private TelephonyManager teleManager;
    private WifiManager wifiManager;
    
    @Override
    public void onCreate() {
        getSystemServices();
        setupBackgroundProcesses();
    }
    
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    
    private void getSystemServices() {
        locListener = new TrackingLocationListener();
        psListener = new TrackingPhoneStateListener();
        
        locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        teleManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    }
    
    /**
     * Initializes data collection procedures. Starts collections location, cell signal, and wifi
     * signal information. Each listener / data collector stores said data in the application
     * SQLite database.
     */
    private void setupBackgroundProcesses() {
        serverSyncer = new ServerSyncerThread();
        serverSyncer.start();
        
        locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
        teleManager.listen(psListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
        
        wifiTracker = new WifiSignalTrackerTask(wifiManager);
        wifiTracker.execute(new Void[]{ });
    }

}
